package com.example.lenovo.sbns;

import android.app.AlertDialog;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.SaveCallback;
import com.example.lenovo.sbns.Activity.Index;
import com.example.lenovo.sbns.Activity.themePage;

import java.util.List;

public class Register extends AppCompatActivity {

    private EditText username,password,sure;
    private Button commit;
    private TextView error;
    private Boolean Flag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        username = (EditText) findViewById(R.id.UserNameRegister);
        password = (EditText) findViewById(R.id.PassWordRegister);
        sure = (EditText) findViewById(R.id.PassWordSure);
        commit = (Button) findViewById(R.id.Commit);
        error = (TextView) findViewById(R.id.Info);

        commit.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(Check()){
                            login();
                        }

                    }
                }
        );

    }

    /**
     * 信息验证
     */
    public boolean Check(){
        if(username.getText().toString().length()==0){
            error.setText("用户名不能为空");
            return false;
        }
        if(password.getText().toString().length()==0){
            error.setText("密码不能为空");
            return false;
        }
        if(sure.getText().toString().length()==0){
            error.setText("请再输入一次密码");
            return false;
        }
        if(password.getText().length()<6){
            error.setText("密码不能少于7位");
            return false;
        }
        if(!(password.getText().toString().equals(sure.getText().toString()))){
            error.setText("两次输入的密码不一样");
            return false;
        }
        return true;
    }

    /**
     * 用户注册函数
     */
    private void login(){

        Flag = true;
        AVQuery<AVObject> query = new AVQuery<>("UserTable");
        query.whereEqualTo("UserName", username.getText().toString());
        query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> list, AVException e) {
                if(list.isEmpty()){
                    Flag = true;
                }
                else{
                    System.out.println(list.get(0).toJSONObject().toString());
                    Flag = false;
                }
            }
        });

        if(Flag) {
            AVObject todo = new AVObject("UserTable");
            todo.put("username", username.getText().toString());
            todo.put("password", password.getText().toString());
            todo.saveInBackground(new SaveCallback() {
                @Override
                public void done(AVException e) {
                    if (e == null) {
                        dialog1();
                    }
                }
            });
        }
        else{
            username.setFocusable(true);
            error.setText("该用户名已经被注册！");
        }

    }

    /**
     * 注册成功对话框
     */
    private void dialog1(){
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("注册信息");
        builder.setMessage("注册成功！");
        builder.setIcon(R.mipmap.ic_launcher);
        builder.setPositiveButton("返回主界面", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss(); //关闭dialog
                Intent intent = new Intent();
                intent.setClass(Register.this, Index.class);
                startActivity(intent);
            }
        });

        builder.create().show();
    }

}
